
#include "lib_sdl2.h"


#define  CONFIG_NOT_USED_SDL2



typedef enum
{
    AUDIO_RUN_INIT = 0,
		
    AUDIO_RUN_CONFIG,
    
    AUDIO_RUN_PLAYING,

    AUDIO_RUN_STOP,

	AUDIO_RUN_IDLE,

} audio_run_state_e;



static pthread_t  pSDL2_play_thread;

static int 	      audio_run_state 	= AUDIO_RUN_INIT;
static int 	      audio_is_init 	= 0;
static uint8_t *  audio_pcm_data  	= NULL;
static int 	      audio_size  		= 0;
static int64_t    audio_pts 	 	= 0;

static SDL_AudioDeviceID 	 audio_device;


void* sdl2_play_thread(void *arg)
{

	while(1)
	{		
		usleep(1000*1000);
		if(!ffmpeg_get_decode_flag()){
			audio_run_state = AUDIO_RUN_INIT;
			continue;
		}

		switch(audio_run_state){
		case AUDIO_RUN_INIT:
		{
			
			
			audio_run_state = AUDIO_RUN_CONFIG;
			break;
		}
		case AUDIO_RUN_CONFIG:
		{
			audio_run_state = AUDIO_RUN_PLAYING;
			break;
		}	
		case AUDIO_RUN_PLAYING:
		{
	

			break;
		}
		case AUDIO_RUN_STOP:
		{

			break;
		}	
		case AUDIO_RUN_IDLE:
		{

			break;
		}				
		default:break;			
		}
	}
}





void audio_play_pcm_callback(void *udata, Uint8 *stream, int len) 
{
	int    ret  = -1;
	int    volume = 0;

	SDL_memset(stream, 0, len);

	volume =  audio_para_get_volume()*SDL_MIX_MAXVOLUME/100;

	//log_i("volume=%d\n",volume);
	
	if(audio_size <= 0)
	{
		ret = queue_audio_pcm_read(&audio_pcm_data,&audio_size,&audio_pts);
		if(ret < 0){
			audio_size = 0;
			audio_pcm_data = NULL;
			return ;
		}

		//log_i("len=%d\n",len);
		//log_i("audio_size=%d\n",audio_size);

		if(audio_size != len)
			log_i("pcm size erro\n");

		len = (len > audio_size ? audio_size : len);

		//SDL_MixAudio(stream, audio_pcm_data, len,volume );
		SDL_MixAudioFormat(stream, audio_pcm_data, AUDIO_S16SYS, len, volume);

		audio_pcm_data = audio_pcm_data + len;
    	audio_size     = audio_size - len;
	}
	else
	{
		len = (len > audio_size ? audio_size : len);

		if(audio_size != len)
			log_i("pcm size erro\n");

		//SDL_MixAudio(stream, audio_pcm_data, len,volume);
		SDL_MixAudioFormat(stream, audio_pcm_data, AUDIO_S16SYS, len, volume);
   		audio_pcm_data = audio_pcm_data + len;
    	audio_size     = audio_size - len;
	}


	return ;
}


int sdl_device_open(void)
{
	int    ret       = -1;
	int    freq      = 0;
	Uint16 format    = 0;
	Uint8  channels  = 0;
	Uint16 samples   = 0;

    SDL_AudioSpec wanted_spec, spec;

	freq     = 44100;//ffmpeg_get_sample_rate();
	format   = 8;//ffmpeg_get_sample_fmt();
	channels = 2;//ffmpeg_get_channels();
	samples  = 1024;//ffmpeg_get_frame_size();


	if(format == 8)
	{
		format = AUDIO_S8;
	}
	
	if(format == 16)
	{
		format = AUDIO_S16SYS;
	}
	
	format = AUDIO_S16SYS;

    wanted_spec.freq 	 = freq;
    wanted_spec.format   = format;
    wanted_spec.channels = channels; 
    wanted_spec.silence  = 0;
    wanted_spec.samples  = samples;
    wanted_spec.callback = audio_play_pcm_callback;
    wanted_spec.userdata = NULL;

	audio_device = SDL_OpenAudioDevice(NULL, 0, &wanted_spec, &spec, SDL_AUDIO_ALLOW_FREQUENCY_CHANGE | SDL_AUDIO_ALLOW_CHANNELS_CHANGE);
	if(!audio_device){
        log_e("can't open audio.\n");
        return -1;
    }

	log_i("sdl open device succeed.\n");

	audio_is_init = 1;
	
	SDL_PauseAudioDevice(audio_device, 0);

	return 0;
}


int sdl_device_close(void)
{
	if(!audio_is_init)
		return -1;

	audio_is_init = 0;

	SDL_CloseAudioDevice(audio_device);
	
	log_i("sdl close device succeed.\n");

	return 0;
}




int lib_sdl2_init(void)
{
	int ret   = 0;

#ifdef CONFIG_NOT_USED_SDL2
	return 0;
#endif

	log_i("lib sdl2 init.\n");

	ret = SDL_Init(SDL_INIT_AUDIO);
	if(ret < 0){
		log_e("sdl init error.\n");
	}else{
		log_i("sdl init succeed.\n");
	}

	
	ret=pthread_create(&pSDL2_play_thread,NULL,sdl2_play_thread,NULL);
	if(ret != 0){
		log_e("thread error=%s\n",strerror(ret));
	}

	sdl_device_open();


	return 0;
}


int lib_sdl2_deinit(void)
{

#ifdef CONFIG_NOT_USED_SDL2
		return 0;
#endif

	log_i("lib sdl2 exit.\n");

	sdl_device_close();

	SDL_Quit();




	return 0;
}
